package com.gxa.modules.pc.hospital.controller;

import com.gxa.common.exception.ResultException;
import com.gxa.common.utils.Result;
import com.gxa.modules.model.dto.ItemsDto;
import com.gxa.modules.model.dto.PackageTypeDto;
import com.gxa.modules.model.entity.hospital.HospitalPark;
import com.gxa.modules.model.entity.hospital.Items;
import com.gxa.modules.model.entity.hospital.Package;
import com.gxa.modules.model.vo.ItemsVo;
import com.gxa.modules.model.vo.PackageVo;
import com.gxa.modules.oss.factory.OSSFactory;
import com.gxa.modules.pc.hospital.service.ItemsService;
import com.gxa.modules.pc.hospital.service.PackageService;
import com.gxa.modules.pc.hospital.service.PackageTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 套餐信息 前端控制器
 * </p>
 *
 * @author gxa
 * @since 2022-11-12 05:04:59
 */
@Api(tags = "项目")
@RestController
@RequestMapping("/pc/Items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("/Items")
    public Result<Map> queryByPage (@RequestBody ItemsVo itemsVo){
        Map<String, Object> stringObjectMap = this.itemsService.queryAll(itemsVo);
        return new Result<Map>().ok(stringObjectMap);
    }

    @ApiOperation(value = "根据套餐类型id查询")
    @ApiImplicitParam(paramType = "path",name = "id",value ="套餐类型id",dataType ="Integer")
    @PostMapping("/ItemsById/{id}")
    public Result<ItemsDto> queryById (@PathVariable("id") Integer id){
        try {
            ItemsDto itemsDto = this.itemsService.queryById(id);
            return new Result<ItemsDto>().ok(itemsDto);
        }catch (Exception e){
            e.printStackTrace();
            return new Result<ItemsDto>().error("查询失败");
        }
    }

    @ApiOperation(value = "根据套餐类型id删除")
    @ApiImplicitParam(paramType = "body", name = "ids", value = "删除的id集合", dataType = "list")
    @DeleteMapping("/ItemsById")
    public Result deleteById (@RequestBody List<Long> ids ){
        try {
            this.itemsService.delById(ids);
            return new Result().ok("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result().error("删除失败");
        }

    }

    @ApiOperation(value = "根据套餐类型id更改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "套餐类型id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "state", value = "套餐类型状态", dataType = "Integer")
    })
    @PutMapping("/ItemsById")
    public Result updateStateByid (@RequestParam Integer id,@RequestParam Integer state){
        try {
            this.itemsService.updateStateById(id, state);
            return new Result().ok("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result<>().error("修改失败");
        }
    }


    @ApiOperation(value = "图片上传")
    @PostMapping("/up")
    public Result<String> upload(@RequestParam("file") MultipartFile file)throws Exception {
        if (file.isEmpty()) {
            throw new ResultException("上传文件不能为空");
        }

        //上传文件
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);

        return new Result<String>().ok(url);
    }

    @ApiOperation(value = "新增套餐")
    @PostMapping("/add")
    public Result add(@RequestBody ItemsDto itemsDto){
        this.itemsService.add(itemsDto);
        return new Result().ok();
    }



}
